package com.example.answer_staff;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.example.answer_staff.api.ApiManager;
import com.gaosai.staff.commlistadapter.ListAndActivityListAdapter;
import com.gaosai.staff.domain.MessageResult;
import com.gaosai.staff.utils.JsonUtils;
import com.gaosai.staff.utils.SPUtil;
import com.gaosai.staff.utils.UseCommInterface;
import com.gaosai.staff.xlistview.XListView;
import com.gaosai.staff.xlistview.XListView.IXListViewListener;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class NewsRoomActivity  extends BaseActivity implements IXListViewListener{
	public static final String NEWS_ITEM="new";
	public static final String ACTIVITIES_ITEM="activity";
	public static final int NEWSROOM_REQUEST = 1;
	private ArrayList<String> news_list ;
	private ListAndActivityListAdapter adapter ;
	private XListView listview_news;
	private int page = 1 ;
	private String tagNames = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//		setContentView(R.layout.newsroom_listview);
		setContentViewItem(R.layout.newsroom_listview) ;
		setupView();
		setListeneers();
		getNewsData();
	}


	private void getNewsData() {
		/**
		 * 展示活动
		 * */

		ApiManager.getInstance().showNews(NewsRoomActivity.this,page,"",tagNames, new AsyncHttpResponseHandler() {
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				Log.e("eee", "开始向服务器请求数据");
				Log.e("tagnames", "tagNames是"+tagNames);
				String result = new String(arg2) ;
				//				Log.e("NewsRoom——-success", result) ;
				MessageResult messageResult = MessageResult.prase(result) ;
				if(0==messageResult.getCode()){
					news_list.addAll(JsonUtils.JsonResult(messageResult.getData())) ;
					adapter.notifyDataSetChanged() ;
					System.out.println("news_list是"+news_list);
				}else{
					Toast.makeText(NewsRoomActivity.this, messageResult.getMsg(), 0).show() ;
				}
			}
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				Log.e("NewsRoom---Faliure", "666666") ;
			}
		}) ;
	}

	private void setupView() {
		showAllImage(R.drawable.menu, R.drawable.filter,R.drawable.search, alignRightOnClickListener, rightOnClickListener) ;
		//解析扫描的时候传递过来的用户信息user_info_list。
		news_list=new ArrayList<String>();
		listview_news=(XListView)findViewById(R.id.listView_newsroom);
		listview_news.setPullRefreshEnable(true);
		listview_news.setPullLoadEnable(true);
		listview_news.setXListViewListener(this) ;
		adapter = new ListAndActivityListAdapter(getApplicationContext(), news_list) ;
		listview_news.setAdapter(adapter);
	}
	private void setListeneers() {

		listview_news.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				String str=news_list.get(position-1);           
				String news_type = getType(str);
				if(news_type.equals("new")){
					Intent intent=new Intent(NewsRoomActivity.this,ListJournalismDetailsActivity.class);
					intent.putExtra("new", str) ;
					startActivity(intent);
				} else{
					//此处是判断显示活动详情的监听来源
					Intent intent=new Intent(NewsRoomActivity.this,ListCampDetailsActivity.class);
					intent.putExtra("new", str) ;
					startActivity(intent);	
				}
			}
			private String  getType(String str) {

				JSONObject jsonObject;
				String type = null ;
				try {
					jsonObject = new JSONObject(str);
					type = jsonObject.getString("types");
				} catch (JSONException e) {
					e.printStackTrace();
				}
				return type;
			}
		});





	}

	@Override
	public void onRefresh() {
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				page = 1 ;
				news_list.clear() ;
				getNewsData() ;
				listview_news.stopLoadMore() ;
				listview_news.stopRefresh() ;
			}
		}, 2000) ;
	}

	@Override
	public void onLoadMore() {
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				page++ ;
				getNewsData() ;
				listview_news.stopRefresh() ;
				listview_news.stopLoadMore() ;
			}
		}, 2000) ;
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.e("eee", "ActivityForResult"+requestCode);

		/**
		 *if处理的情况是fliterActivity界面的返回操作
		 *else 处理的情况是ListCampActivity界面返回的操作
		 * 
		 */
		if(resultCode==RESULT_OK){
			//再判断返回来的情况，是成功还是失败等等	 resultCode
			Log.e("eee", "newsroom中的resultCode"+requestCode);
			switch(requestCode){
			case NEWSROOM_REQUEST:
				String tagNames_list = SPUtil.getValue(getApplicationContext(), SPUtil.TAGNAMES);
				String tagNames_list_substring = tagNames_list.substring(tagNames_list.indexOf("[")+1,tagNames_list.lastIndexOf("]"));
				Spilt(tagNames_list_substring);
				Log.e("eee", "Spilt的tagename是"+tagNames);	
				page =1 ;
				news_list.clear() ;
				getNewsData();	
				break;
			}
		}
	}


	private void Spilt(String tagNames_list_substring) {
		StringBuilder sb=new StringBuilder();
		String[] tagNames_list_spilt = tagNames_list_substring.split(",");
		for (String string : tagNames_list_spilt) {
			String string_spilt = string;
			sb.append(string_spilt.trim()).append(",");
		}
		tagNames=sb.deleteCharAt(sb.length()-1).toString();

	}



	private View.OnClickListener rightOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			UseCommInterface.getInstance().startActivity(NewsRoomActivity.this, SearchNowsListActivity.class, null) ;
		}
	};

	private View.OnClickListener alignRightOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			UseCommInterface.getInstance().startActivityForResult(NewsRoomActivity.this, NEWSROOM_REQUEST, FliterActivity.class, null) ;
		}
	};
}
